﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class ShipEnginePSystem2 : PointSpriteParticleSystem
    {
        public override bool IsAlive
        {
            get
            {
                return elapsedSystemTimeMs < MaxLifeTimeMs;
            }
        }

        public ShipEnginePSystem2(Scene scene, Vector3 position)
            : base(scene, position)
        {
        }

        protected override void InitParameters()
        {
            textureName = "Textures\\8195-v6";
            effectName = "Effects\\RegularPSystem";
            effectTechName = "AdditiveBlendingSystem";

            Acceleration = -0.00f;
            maxParticles = 25;

            originalEmitTimeMs = 30.0f;

            emitter = new ParticleEmitter(this, originalEmitTimeMs, position);

            MinInitialSpeed = 0.0f;
            MaxInitialSpeed = 0.0f;
            MinInitialSize = 600;
            MaxInitialSize = 800;
            MinLifeTimeMs = 150;
            MaxLifeTimeMs = 250;

            color = Color.White;
        }

        protected float currentStrength = 4.0f;
        protected float maxStrength = 12.0f;
        protected float strengthROC = 0.02f; // ROC = rate of change

        protected float originalEmitTimeMs;
        protected float emitTimeMsModFactor = 1.5f;

        public void Update(GameTime gameTime, Vector3 position, Matrix rotation,
            Vector3 velocity, bool speedingUp)
        {
            if (speedingUp && currentStrength < maxStrength)
            {
                currentStrength += strengthROC * gameTime.ElapsedGameTime.Milliseconds;
                if (currentStrength > maxStrength)
                    currentStrength = maxStrength;
            }
            else if (!speedingUp && currentStrength > 4.0f)
            {
                currentStrength -= strengthROC * gameTime.ElapsedGameTime.Milliseconds;
                if (currentStrength < 4.0f)
                    currentStrength = 4.0f;
            }

            emitter.EmitTimeMs = originalEmitTimeMs - currentStrength * emitTimeMsModFactor;

            base.Update(gameTime, position, rotation, velocity);
        }

        protected override void InitParticle(int index, Vector3 position, float initialTimeMsOffset)
        {

            base.InitParticle(index, Vector3.Zero, initialTimeMsOffset);
            vertices[index].InitialPosition = position;
            float speed = RandomHelper.RandomBetween(MinInitialSpeed, MaxInitialSpeed);
            vertices[index].InitialVelocity =
                Vector3.Transform(Vector3.Backward, rotation) * currentStrength + this.velocity;
        }

    }
}
